package com.ruoyi.common.core.domain;

import com.ruoyi.common.enums.SignEnum;
import org.springframework.context.ApplicationEvent;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

/**
 * @Classname BaseEvent
 * @Author: sunluquan
 * @Date: 2021/12/24 09:10
 * @Description TODO
 * 事件发布基类
 */
public abstract class BaseEvent<T> extends ApplicationEvent {

    protected Object source;

    /** false：前端触发；true：后端触发*/
    protected boolean operationType;

    protected SignEnum signEnum;

    public BaseEvent(T source, SignEnum signEnum,boolean operationType) {
        super(source);
        this.source = source;
        this.signEnum = signEnum;
    }

    public BaseEvent(T source, SignEnum signEnum) {
        this(source,signEnum,false);
    }

    public BaseEvent(Collection<T> sources, SignEnum signEnum,boolean operationType) {
        super(sources);
        this.source = sources;
        this.signEnum = signEnum;
        this.operationType = operationType;
    }

    public BaseEvent(Collection<T> source, SignEnum signEnum) {
        this(source,signEnum,false);
    }
    //是否为集合容器
    public boolean isContainer(){
        if(source instanceof Collection || source instanceof Map){
            return true;
        }
        return false;
    }
    @Override
    public T getSource() {

        return (T) source;
    }

    public Collection<T> getSources(){
        if(source instanceof Collection){
            return (Collection<T>) source;
        }
        return (Collection<T>) Arrays.asList(source);
    }

    public SignEnum getSignEnum() {
        return signEnum;
    }

    public boolean isOperationType() {
        return operationType;
    }
}
